package cluster.clustersingleton

import akka.actor.ActorSystem
import akka.event.LoggingAdapter
import akka.http.scaladsl.server.Directives
import akka.stream.Materializer
import com.typesafe.config.Config
import scala.concurrent.ExecutionContextExecutor

/**
  * Created by shouf on 2016/5/19.
  */
trait PostsOperations extends Directives with JsonSupport {
  implicit val system:ActorSystem

  implicit def executor: ExecutionContextExecutor

  implicit val materializer: Materializer

  def config: Config

  val logger: LoggingAdapter

  lazy val postsRoutes={
    getRoute ~ postRoute
  }

  private val getRoute = logRequestResult("posts-get"){
    path("api" / "v1" / "posts" / Segment) { postid =>
      get {
        complete {
          Map("postid" -> postid)
        }
      }
    }
  }

  private val postRoute = logRequestResult("posts-post"){
    path("api" / "v1" / "posts") {
      post {
        entity(as[Posts]) { posts =>
          complete {
            //回覆物件做Json序列化
            Map("posts.text" -> posts.text)
            //回覆成字串
            //s"Posts.text: ${posts.text}"
          }
        }
      }
    }
  }
}
